/**
 * Copyright (c) 2006 RiseSoft Co.,Ltd  
 * $Header$
 */
package cn.newtest.bi.collection.formula.runtime;

import groovy.lang.GroovyObject;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import net.risesoft.risebi.collection.groovy.Fault;
import net.risesoft.risebi.collection.groovy.GroovyLoader;
import net.risesoft.risebi.collection.model.Formula;
import net.risesoft.risebi.collection.model.RangeObj;
import net.risesoft.risebi.collection.model.ReportData;
import net.risesoft.risebi.collection.model.ReportInstance;
import net.risesoft.risebi.collection.model.Template;
import net.risesoft.risebi.collection.model.User;
import net.risesoft.risebi.collection.service.DataOperationManager;
import net.risesoft.risebi.collection.service.TemplateManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 *
 * @author <a href="mailto: chenjian@risesoft.net">陈健</a>
 * @version $Revision$
 * 
 */
@Repository(value="auditor")
public class DefaultAuditor implements Auditor {
	
	private GroovyLoader groovyLoader;
	private TemplateManager templateManager;
	private DataOperationManager dataOperationManager;

	@Autowired
	public void setDataOperationManager(DataOperationManager dataOperationManager) {
		this.dataOperationManager = dataOperationManager;
	}
	
	@Autowired
	public void setTemplateManager(TemplateManager templateManager) {
		this.templateManager = templateManager;
	}

	@Autowired
	public void setGroovyLoader(GroovyLoader groovyLoader) {
		this.groovyLoader = groovyLoader;
	}

	/*
	 * (non-Javadoc)
	 * @see net.risesoft.risebi.collection.formula.runtime.Auditor#doAudit(java.lang.String, net.risesoft.risebi.collection.model.ReportInstance, net.risesoft.risebi.collection.model.ReportData, net.risesoft.risebi.collection.model.User)
	 */
	public Map<String,List<Fault>> doAudit(String templateGuid,ReportInstance ri,ReportData rd,User user) throws Exception {
//		String templateFilePath = FilePersistLocator.getFilePersistLocator(SystemConstants.REPORT_TEMPLATEFILE);
//		templateFilePath += templateGuid+"/";
//		String fileName = templateFilePath + "G"+templateGuid+"Audit.groovy";
//		
//		File file = new File(fileName);
//		if(!file.exists()){
//			return new HashMap<String,List<Fault>>();
//		}
		
		GroovyObject go = groovyLoader.getGroovyObject(templateGuid,"audit");
		
		if(go==null)
			return new HashMap<String,List<Fault>>();
		
		go.setProperty("ri", ri);
		go.setProperty("rd", rd);
		go.setProperty("user", user);
		
		
		
//		组建每个range中对应的catalogName
//		DataOperationManager dataOperationManager = (DataOperationManager) ContextUtil.getAc().getBean("dataOperationManager");
		Map<String,String> rangeCatalogNameMap = dataOperationManager.makeRangeCatalogName(templateGuid);
		go.setProperty("rangeCatalogNameMap", rangeCatalogNameMap);
		
//		组建RangeSortedCotalogCodeMap
		Map<String,RangeObj> ranges = rd.getRanges();
		if(ranges!=null){
			Map<String,List<String>> rangeSortedCotalogCodeMap = dataOperationManager.makeRangeSortedCotalogCodeMap(rd.getRanges());
			go.setProperty("rangeSortedCotalogCodeMap", rangeSortedCotalogCodeMap);
		}
		
		Template template = (Template) templateManager.getObject(Template.class, templateGuid);
		List<Formula> formulas = template.getFormulas();
		
		Map<String,List<Fault>> containedFaultMap = new TreeMap<String,List<Fault>>(new CollatorComparator());
		for(int i=0;i<formulas.size();i++){
			List<Fault> containedFaults = new ArrayList<Fault>();
			
			Formula formula = formulas.get(i);
			String formulaName = formula.getName();
			if(formula.getType()!=Formula.TYPE_JISUANGONGSHI){
				String formulaGuid = "f"+formula.getGuid();
				Object[] args = {};
		        go.invokeMethod(formulaGuid, args);
		        
		        
		        List<Fault> faults = (List<Fault>) go.getProperty("faults");
		        for(int j=0;j<faults.size();j++){
		        	Fault fault = faults.get(j);
		        	
		        	fault.setFormulaName(formulaName);
		        	
		        	if(formula.getType()==Formula.TYPE_QIANGZHISHENHE){
		        		fault.setFormulaType("强制性审核");
		        	}else{
		        		fault.setFormulaType("一般性审核");
		        	}
		        	containedFaults.add(fault);
		        }
			}
			
			if(!containedFaults.isEmpty()){
				containedFaultMap.put(formula.getGuid()+"###"+formula.getType(), containedFaults);
			}
		}
		
		return containedFaultMap;
	}
	
	class CollatorComparator implements Comparator<String> {
		public int compare(String o1, String o2) {
			String[] coll = o1.split("###");
			int i1 = new Integer(coll[1]);
			coll = o2.split("###");
			int i2 = new Integer(coll[1]);
			return i1-i2==0?1:i1-i2;
		}
	}
}
